<?php
/**
 * Created by PhpStorm.
 * User: Hk
 * Date: 2021/1/26
 * Time: 18:04
 */

namespace App\Http\Controllers\Estate;


use App\Http\Controllers\Controller;
use App\Models\ElectricCommunity;
use App\Models\ElectricElectricity;
use App\Models\ElectricMeterReadingLog;
use App\Models\ElectricPaymentOrder;
use App\Models\EstateCommunity;
use App\Models\ElectricPlatformAgent;
use App\Services\SysRegion;
use App\Traits\HelperTrait;
use Illuminate\Http\Request;

class ElectricCommunityController extends Controller
{

    public $model;

    public function __construct()
    {
        $this->model = new EstateCommunity();
    }

    /**
     * Notes: 小区列表
     * User: xiaohui
     * Date: 2021/3/17
     * Time: 14:12
     * email: 363876982@qq.com
     * @param Request $request
     */
    public function index(Request $request)
    {
        $param = $request->query();
        $where[] = ['is_deleted', 0];
        if (isset($param['name']) && !empty($param['name'])) $where[] = ['name', 'like', "%{$param['name']}%"];
        if (isset($param['areas']) && !empty($param['areas'])) {
            [$province, $city, $area] = $param['areas'];
            $where[] = ['county', '=', $area];
        }
        if (isset($param['agent']) && !empty($param['agent'])) $where[] = ['agent_id', $param['agent']];
        $lsit=EstateCommunity::query()
                ->with(['platform_agent','SysRegionProvince','SysRegionCity','SysRegionCounty'])
                ->where($where)
                ->orderBy('id', 'desc')
                ->paginate($request->per_page ?? 30);

        return $this->success($lsit);
    }

    /**
     * Notes: 获取省市区三级联动数据接口
     */
    public function sys_region(SysRegion $sys_region)
    {
        $list = $sys_region->getAreas()['data'];
        return $this->success($list);
    }
    public function getAgent(ElectricPlatformAgent $ElectricPlatformAgent)
    {
        $list = $ElectricPlatformAgent->where('status',1)->select('agent_name','id')->get()->toArray();
        return $this->success($list);
    }
    public function EditElectricCommunity($id = null)
    {

        $post = $_POST;
        if ($post)
        {
            if (isset($post['areas']))
            {
                $data['province'] = $post['areas'][0];
                $data['city'] = $post['areas'][1];
                $data['county'] = $post['areas'][2];
            }
            $data['name'] = $post['name'];
            $data['address'] = $post['address'];
            $data['agent_id'] = $post['agent_id'];
            $data['update_time'] = date("Y-m-d H:i:s", time());
            if (isset($post['id'])) {
                $info = EstateCommunity::query()
                    ->where('id', $post['id'])
                    ->update($data);
            }else{
                $info = EstateCommunity::query()
                    ->insert($data);
            }

        }else{
            $info=EstateCommunity::query()
                ->with(['platform_agent'])
                ->where('id',$id)
                ->first();
        }

        return $this->success($info);
    }
    public function delElectricCommunity($id,Request $request)
    {
        $ids=explode(',',$id);
        $query = $this->model
            ->whereIn('id' , $ids)
            ->update(['is_deleted'=>1]);
        return $this->success($query);
    }
    public function editElectricCommunityStatus()
    {

        $data=$_POST;
        if ($data){
            $info = $this->model
                ->where('id', $data['id'])
                ->first();
            if ($info[$data['type']] == 0)
            {
                $query = $this->model
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>1]);

            }else if ($info[$data['type']] == 1){

                $query = $this->model
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>0]);
            }
            return $this->success('修改成功!');
        }
        return $this->error('修改失败');
    }
    public function ElectricLineLoss(Request $request)
    {
        $param = $request->post();
        $where[] = ['community',$param['id']];

        $lsit=ElectricElectricity::query()
            ->with(['electric_collector'])
            ->where($where)
            ->paginate($request->per_page ?? 30);
        $name=ElectricCommunity::query()
            ->where('id',$param['id'])->select('name')->first();
            foreach ($lsit as $k=>$v)
            {
                if (isset($param['change'])) {
                    $last_log=ElectricMeterReadingLog::query()
                        ->where('meter_id',$v['id'])
                        ->orderBy("create_time", "desc")
                        ->whereDate('create_time', '>=',
                            date("Y-m-d H:i:s",strtotime($param['change'][0])))
                        ->whereDate('create_time', '<=',
                            date("Y-m-d H:i:s",strtotime($param['change'][1])))
                        ->select('day_count','day_cost','is_right','last_time', 'line_count')
                        ->first();
                }else{
                    $last_log=ElectricMeterReadingLog::query()
                        ->where('meter_id',$v['id'])
                        ->orderBy("create_time", "desc")
                        ->select('day_count','day_cost','is_right','last_time', 'line_count')
                        ->first();
                }

                if (empty($last_log) || $last_log['day_cost'] == null || $last_log['day_count'] == null || $last_log['is_right'] == null) {
                    //查询是内无合适数据
                    $lsit[$k]['ele'] = 0;
                    $lsit[$k]['cost'] = 0;
                    $lsit[$k]['is_right'] = 2;
                    $lsit[$k]['line_count']=0;
                    $lsit[$k]['last_time']=date('Y-m-d H:i:s',0);

                }else{
                    $lsit[$k]['ele'] = $last_log['day_cost'];
                    $lsit[$k]['cost'] = $last_log['day_count'];
                    $lsit[$k]['is_right'] = $last_log['is_right'];
                    $lsit[$k]['line_count']=$last_log['line_count'];
                    $lsit[$k]['last_time']=date('Y-m-d H:i:s',$last_log['last_time']);
                }

            }
            $data['name'] = $name;
            $data['list'] = $lsit;
        return $this->success($data);
    }
}
